package com.leyou.item.service;

import com.leyou.item.entity.Item;
import com.leyou.item.exceptions.LyException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class ItemService {

    int id= 1;

    public Item saveItem(Item item){
        // 判断价格是否为空
        if(item.getPrice() == null){
            throw new LyException(400,"价格不能为空！");
        }
        if(item.getName() == null){
            throw new LyException(401,"名称不能为空！");
        }

        // 自增长生成id，模拟数据库的新增
       item.setId(id++);
        if (id==4){
            throw new RuntimeException("数据库新增失败");
        }
        // 返回新增之后的对象，回显id
        return item;
    }
}